package com.android.SAXParser;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ChannelActivity extends ListActivity {
	
//	MySQLiteHelper SQLhelp;

//	ArrayList<String> about = new ArrayList<String>();

	ArrayList<String> dagskrain;
	XMLGettersSetters data;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
//		SQLhelp = new MySQLiteHelper(this);
		super.onCreate(savedInstanceState);
		String channelName = getIntent().getStringExtra("ChannelName");
		this.setTitle(channelName);

		dagskrain = new ArrayList<String>();
		Bundle extras = getIntent().getExtras();
		//dagskrain = extras.getStringArrayList("about");
		String Link = extras.getString("Link");
		String dags = extras.getString("dagsetning");
		
		try {
			SAXParserFactory saxPF = SAXParserFactory.newInstance();
			SAXParser saxP = saxPF.newSAXParser();
			XMLReader xmlR = saxP.getXMLReader();

			URL url = new URL("http://www.vodafone.is" + Link);
			Log.d("urlid",url.toString());
			Log.d("urlid",url.toString());

			XMLHandler_channel myXMLHandler = new XMLHandler_channel();
			xmlR.setContentHandler(myXMLHandler);
			xmlR.parse(new InputSource(url.openStream()));
			}
			catch (Exception e) {
				System.out.println(e);
			}
			data = XMLHandler_channel.data;
				Log.d("getTitleSize",String.valueOf(data.getTitle().size()));
				for (int i = 0; i < data.getTitle().size(); i++) {
					if (data.getDate().get(i).equals(dags)) 
					{
						dagskrain.add(data.getTime().get(i) + "  "+ data.getTitle().get(i));
					}
				}
				
			Intent intentDagskra = new Intent().setClass(this,ChannelActivity.class);

			intentDagskra.putExtra("Link", Link);
		//	intentDagskra.putStringArrayListExtra("about", about);
			
	
			    
		
		Log.d("channelactivity","channelactivity");
		Log.d("dagskrain",dagskrain.get(0));

		ListView listView = getListView();
 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dagskrain);
 		listView.setAdapter(adapter); 

		listView.setTextFilterEnabled(true);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
//		String aboutTheProgram = about.get((int) id);
		String nafn = dagskrain.get((int) id);
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(nafn);
	//	alertDialog.setMessage(aboutTheProgram);
		alertDialog.show();
		alertDialog.setCanceledOnTouchOutside(true);

	}



}
